package de.breitbeil.jbooking.ui.view.tablecellrenderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EmailTableCellRenderer extends DefaultTableCellRenderer {

    private Log logger = LogFactory.getLog(EmailTableCellRenderer.class);
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        final Desktop desktop = Desktop.getDesktop();
        if(desktop.isSupported(Desktop.Action.MAIL)) {
            
            if(!isSelected) {
                setForeground(Color.BLUE);
            }
            
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    URI uriMailTo = null;
                    try {
                        uriMailTo = new URI("mailto", getText(), null);
                        desktop.mail(uriMailTo);
                    }
                    catch(Exception ex) {
                        logger.error("Mailto failed", ex);
                    }
                }
            });
        }
        
        return this;
    }
    
}
